//
//  DefaultSettingVC.m
//  NtoccShipper
//
//  Created by mac on 2019/11/28.
//  Copyright © 2019 zhuchongjin. All rights reserved.
//

#import "DefaultSettingVC.h"
#import "SendGoodsCell.h"
#import "SelectGoodsNameVC.h"
#import "SelectGoodsNameCellModel.h"
#import "SelectDriverVC.h"
#import "DriverListModel.h"
#import "UseCarTypesVC.h"
#import "DefaultSettingModel.h"

@interface DefaultSettingVC ()<UITableViewDelegate,UITableViewDataSource>{
    
//    FlexFrameView *header;
}
@property (nonatomic,strong) FlexFrameView *header;
@property (nonatomic,strong) CJTableView *tableView;
@property (nonatomic,strong) SendGoodsCell *sendGoodsCell;

@property (strong, nonatomic) CJTextField *fieldGoodsName;
@property (nonatomic,copy) NSDictionary *dicGoodsName;
@property (nonatomic,copy) NSString *strGoodsNameCodes; //货物名称的code
@property (nonatomic,copy) NSString *strGoodsSmallName;
@property (nonatomic,copy) NSString *strGoodsBigTypeCode; //货物大类code
@property (nonatomic,copy) NSString *strGoodsBigTypeName;


@property (nonatomic,strong) CJTextField *fieldGoodsNum; // 货物数量
@property (nonatomic,strong) CJLabel *labGoodsTransferFeeUnits;//货物数量单位
@property (nonatomic,copy) NSString *strGoodsTransferFeeUnits; //货物单位 0：吨 1：立方米，2-件 ,


@property (nonatomic,strong) CJLabel *labGoodsNumUnit;
 
@property (strong, nonatomic) CJTextField *fieldGoodsPrice; // 单价
@property (strong, nonatomic) CJLabel *labGoodsPriceUnit;

@property (strong, nonatomic) CJTextField *fleidLoss; //损耗
@property (strong, nonatomic) CJLabel *labLossUnit;
@property (nonatomic,copy) NSString *strLossCode;


@property (strong, nonatomic) CJTextField *fieldFreightCost; // 运费单价
@property (strong, nonatomic) CJLabel *labFreightCostUnit;
@property (nonatomic,copy) NSString *strFreightCostUnit;
@property (nonatomic,strong) FlexTouchView *touchViewLoss;


//@property (strong, nonatomic) CJTextField *fieldCarNumsRequir; // 车数要求

//@property (strong, nonatomic) CJTextField *fieldUpLoadGoodsTime; //装车时间
//@property (strong, nonatomic) CJTextField *fieldDomnLoadCoodsTime; //卸车时间

//@property (strong, nonatomic) CJLabel *labDistance; // 距离

//@property (nonatomic,strong) UISwitch *switchInSure; // 投保
@property (nonatomic,strong) UISwitch *switchInSure;
@property (nonatomic,copy) NSString *strIsInsure; //投保类别：0-不投保 1

@property (strong, nonatomic) CJTextField *fieldInSureMoney; // 投保金额
@property (nonatomic,copy) NSString *strInSureMoneyCode;

//@property (strong, nonatomic) CJTextField *fieldAppointDriver; //指定司机
@property (nonatomic,copy) NSString *strAppointDriverCode; // 指定司机code

@property (strong, nonatomic) CJTextField *fieldUserCarType; //用车类型
@property (nonatomic,copy) NSDictionary *dicUserCarType;

////迭代去掉 21-08-13
//@property (strong, nonatomic) CJTextField *fieldOpenInvoiceType;  // 开票方式
//@property (nonatomic,copy) NSString *strOpenInvoiceType;  //开票方式名称：0-现付开票，1-到付开票，2-代付开票 ,

//@property (strong, nonatomic) CJTextField *fieldUpstreamCustom; // 上游客户
//@property (strong, nonatomic) CJView *viewShoudCollectMoney;
//@property (strong, nonatomic) CJTextField *fieldShoudCollectMoney; //应收
@property (nonatomic,strong) CJTextField *fieldTranRequirement; //运输要求

@property (strong, nonatomic) CJTextField *fieldHighestPrice;
@property (strong, nonatomic) CJLabel *labHighestPriceUnit;  //最高价单位

//@property (nonatomic,strong) CJView *viewOthers;
//@property (nonatomic,strong) CJLabel *attrLabel;


//@property (nonatomic,strong) UISwitch *switchCommonSource;
@property (nonatomic,strong) UISwitch *switchIsAllowPrice; // 是否允许报价
@property (nonatomic,copy) NSString *strIsQuote; //0-不允许，1-允许 ,
@property (nonatomic,strong) UIView *viewHighestPrice;
//@property (nonatomic,copy) NSString *strViewOthersIsHidden; // 0否 1 是

// 协议

//@property (nonatomic,strong) CJImageView *imgVAgree;
//@property (nonatomic,copy) NSString *strIsAgree; // 0 1
//
//@property (nonatomic,copy) NSString *strIsOftenOrder; // 是否常发货源 是否常发货源 0->否，1->是

@property (nonatomic,strong) UIView *viewInSureMoney; //
@property (nonatomic,strong) DefaultSettingModel *model;

@property(nonatomic ,assign)BOOL isChange;

@end

@implementation DefaultSettingVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self setTitleName:@"默认设置"];
    [self setBackLeftButton:@""];

//    [self setSecondAppNavigationBarStyle];

    self.strIsQuote = @"0";
    self.sendGoodsCell = [[SendGoodsCell alloc]initWithFlex:nil reuseIdentifier:nil];
    [self.view addSubview:self.tableView];
    [self createTableView];
    self.isChange = NO;
    [self setDefaultValue];
    [self setFieldLimit];
    [self loadOrederDefaultACtion];
    

}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:YES];
//    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault;
    self.massageButton.titleLabel.font = [UIFont cjTitleFont14];
    [self.massageButton setTitleColor:[UIColor cjSubheadTextColor] forState:UIControlStateNormal];

}



- (void)loadOrederDefaultACtion{
    
    NSString *strURL = CJStringWithFormat(@"%@/ntocc-tms-api/orderDefaultInfo/default",CJBaseUrl);

    WS(ws);
    NSDictionary *dic = [[NSDictionary alloc]init];
    [[HDNetworking sharedHDNetworking] GET:strURL parameters:dic success:^(id  _Nonnull responseObject) {
        
        DLog(@"++++++++%@",responseObject);
        NSDictionary *dic = (NSDictionary *)responseObject;
        if (dic.count == 0) {
            
        }else{
            NSError *error;
            self.model = [[DefaultSettingModel alloc]initWithDictionary:dic error:&error];
            [ws refreshData];
            ws.isChange = YES;
        }

    } failure:^(NSError * _Nonnull error) {
        DLog(@"++++++++%@",error);
        
    }];

}

- (void)refreshData{
   
    
//    self.dicGoodsName = [[NSDictionary alloc]init];
//    self.dicUserCarType = [[NSDictionary alloc]init];
    // 名称
//    WS(ws);
//    NSArray *list = self.model.coalTypeList;
//    if (list.count > 0) {
//        [list enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
//            NSArray *listCode = (NSArray *)obj;
//            if (idx == 0) {
//                ws.strGoodsNameCodes = listCode[0];
//            }else{
//                ws.strGoodsNameCodes = CJStringWithFormat(@"%@,%@",ws.strGoodsNameCodes,listCode[0]);
//            }
//        }];
//    }else{
//        self.strGoodsNameCodes = @"";
//    }
//
    self.strGoodsNameCodes = self.model.coalType;
    
    self.fieldGoodsName.text = self.model.coalTypeName;
    self.strGoodsBigTypeCode = self.model.coalBigType;
    self.strGoodsBigTypeName = self.model.coalBigTypeName;

    self.fieldGoodsNum.text = @(self.model.weight).cj_description;
    self.strGoodsTransferFeeUnits  = self.model.coalUnit;
    self.labGoodsTransferFeeUnits.text = self.model.coalUnitName;

    if ([self.model.coalUnit intValue] == 2) {
        [self setFieldGoodsNumThreeDecimalPlace:NO];
    }else{
        [self setFieldGoodsNumThreeDecimalPlace:YES];
    }
    
    
    self.fieldGoodsPrice.text = @(self.model.coalPrice).cj_money_description;
    self.labGoodsPriceUnit.text = self.model.isTrunkName;
    self.strLossCode = CJStringWithFormat(@"%ld",(long)self.model.limitWastageType); // 损耗 默认1 kg/che
    self.fleidLoss.text = CJStringWithFormat(@"%@",self.model.limitWastageAmount);
    self.labLossUnit.text = self.model.limitWastageTypeName;
    
    DLog(@"======== %@",[self.strLossCode substringFromIndex:0]);
    
    if ([[self.strLossCode substringFromIndex:0] isEqualToString:@"0"]) {
        self.fleidLoss.enabled = NO;
        self.fleidLoss.backgroundColor = [UIColor cjBackgroundColor];
    }else{
        self.fleidLoss.enabled = YES;
        self.fleidLoss.backgroundColor = [UIColor cjWhiteColor];
    }
    
    if (self.model.isTrunk == 3) {
        self.fleidLoss.text = @"0";
        self.labLossUnit.text = @"不计";
        self.fleidLoss.enabled = NO;
        self.fleidLoss.backgroundColor = [UIColor cjBackgroundColor];
//        self.touchViewLoss.userInteractionEnabled = NO;
    }else{
//        self.touchViewLoss.userInteractionEnabled = YES;

    }
    
    
    
    self.strFreightCostUnit = CJStringWithFormat(@"%ld",(long)self.model.isTrunk);
    self.labFreightCostUnit.text = self.model.isTrunkName;
    self.fieldFreightCost.text = @(self.model.price).cj_money_description;
    
    if (self.model.isInsure == 1) {
        self.strIsInsure = @"1"; //投保类别：0-不投保
        self.switchInSure.on = YES;
        self.strInSureMoneyCode =  CJStringWithFormat(@"%@",self.model.insuranceCode);
        self.fieldInSureMoney.text = self.model.insuranceFeeName;
    }else if(self.model.isInsure == 0){
        self.strIsInsure = @"0"; //投保类别：0-不投保
        self.switchInSure.on = NO;
        self.strInSureMoneyCode =  @"0";
        self.fieldInSureMoney.text = @"";
    }
    

    self.strAppointDriverCode = self.model.pushTarget; // 指定司机
//    self.fieldAppointDriver.text = self.model.pushTargetName;
    
    
    self.fieldUserCarType.text = self.model.pushVehicleUse;
//    迭代去掉 21-08-13
//    self.strOpenInvoiceType  =  CJStringWithFormat(@"%ld",(long)self.model.billingType);
//    self.fieldOpenInvoiceType.text = self.model.billingTypeName;
    self.fieldTranRequirement.text = self.model.coalName;
    
    if (self.model.isQuote == 0) {
        self.strIsQuote = @"0";
        self.switchIsAllowPrice.on = NO;
        self.fieldHighestPrice.text  = @"";
    }else if(self.model.isQuote == 1){
        self.strIsQuote = @"1";
        self.switchIsAllowPrice.on = YES;
        self.fieldHighestPrice.text  =  @(self.model.maxPrice).cj_money_description;
    }

    [self createTableView];
}


/// 创建tableview 等
- (void)createTableView{

//    WS(ws);
    self.header.flexibleHeight = YES;

//    strIsInsure; //投保类别：0-不投保 1
    if (self.strIsInsure.intValue == 0) {
        self.viewInSureMoney.hidden = YES;
    }else{
        self.viewInSureMoney.hidden = NO;
    }
    
    //0-不允许，1-允许 ,报价
    if (self.strIsQuote.intValue == 0) {
        self.viewHighestPrice.hidden = YES;
    }else{
        self.viewHighestPrice.hidden = NO;
    }
    
    self.tableView.tableHeaderView = self.header;
    [self.tableView reloadData];

}

- (FlexFrameView *)header{
    if (!_header) {
        CGRect rcFrame = CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width, 0);
        _header = [[FlexFrameView alloc] initWithFlex:@"DefaultSettingHeaderView" Frame:rcFrame Owner:self];

        _header.flexibleHeight = YES;
        // 是否投保
        [self.switchInSure addTarget:self action:@selector(switchInSureAction:)forControlEvents:UIControlEventValueChanged | UIControlEventTouchDragExit];
        self.switchInSure.transform = CGAffineTransformMakeScale(0.75, 0.75);
        self.switchInSure.onTintColor = [UIColor cjMainColor];

        //是否允许报价
        [self.switchIsAllowPrice addTarget:self action:@selector(switchIsAllowPriceAction:)forControlEvents:UIControlEventValueChanged | UIControlEventTouchDragExit];
        self.switchIsAllowPrice.transform = CGAffineTransformMakeScale(0.75, 0.75);
        self.switchIsAllowPrice.onTintColor = [UIColor cjMainColor];

        [_header layoutIfNeeded];
    }
    return _header;
}

#pragma mark ----------点击事件----

/// 货物名称
- (void)btnSelectGoodsNameAction{
//    [CJAlert showStringDismissWithDelay:@"货物名称"];

    WS(ws);
    SelectGoodsNameVC *vc = [[SelectGoodsNameVC alloc]init];
    if (IsNilOrNull(self.dicGoodsName)) {
        vc.dictData = [[NSDictionary alloc]init];
    }else{
        vc.dictData = self.dicGoodsName;
    }
    
    vc.blockSelectInfo = ^(id anyObject) {
        DLog(@"++++++++ %@",anyObject);
        ws.dicGoodsName = (NSDictionary *)anyObject;
    
        NSArray *list = ws.dicGoodsName[@"cellInfo"];
        ws.fieldGoodsName.text = @"";
        NSString *strCodes = @"";
        for (int i = 0; i < list.count; i++) {
            
            NSArray *array = (NSArray *)list[i];
            for (int y = 0; y < array.count; y++) {
                SelectGoodsNameCellModel *mod = (SelectGoodsNameCellModel *)array[y];
            
                if (mod.selected) {
//                     DLog(@"========= %@",mod.name);
                    ws.fieldGoodsName.text = CJStringWithFormat(@"%@,%@",self.fieldGoodsName.text,mod.name);
                    strCodes = CJStringWithFormat(@"%@,%@",strCodes,mod.code);
                }
            }
        }
        
        ws.strGoodsNameCodes = [strCodes substringFromIndex:1];
        ws.fieldGoodsName.text = [ws.fieldGoodsName.text substringFromIndex:1];
        ws.strGoodsBigTypeCode = anyObject[@"coalBigType"];
        ws.strGoodsBigTypeName = anyObject[@"name"];
    };
    [self pushVc:vc];
}
- (void)btnSelectGoodsNumUnitAction{
//    [CJAlert showStringDismissWithDelay:@"货物数量单位"];
    [self btnSelectGoodTypeDicts:CJGetSendgoodsTransferFeeUnits];

}
- (void)btnGoodsPriceUnitAction{
//    [CJAlert showStringDismissWithDelay:@"货物jiage单位"];
}

//损耗单位
- (void)btnGoodsLossUnitAction{
//    [CJAlert showStringDismissWithDelay:@"损耗单位"];
//    [self btnSelectGoodTypeDicts:CJGetSendgoodslimitWastageType];
    NSString *strUrl = CJStringWithFormat(@"%@/ntocc-basic-api/dict/listDict",CJBaseUrl);
    NSDictionary *dicPra = @{@"types":CJStringWithFormat(@"limitWastageType%@",self.strGoodsTransferFeeUnits)
                            };
    WS(ws);
    [[HDNetworking sharedHDNetworking] GET:strUrl parameters:dicPra success:^(id  _Nonnull responseObject) {

        DLog(@"==== %@",responseObject);
        NSDictionary *dic = responseObject[0];
        NSMutableArray *arrMutar = [[NSMutableArray alloc]init];
        NSMutableArray *arrMutarCodes = [[NSMutableArray alloc]init];

        [dic[@"dictList"] enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
            
            [arrMutar addObject:obj[@"name"]];
            [arrMutarCodes addObject:obj[@"code"]];
            DLog(@"===== %@",arrMutarCodes);
        }];
        [ws btnSelectList:arrMutar listCodes:arrMutarCodes title:dic[@"name"]];
    } failure:^(NSError * _Nonnull error) {

    }];
    
}


- (void)btnSelectList:(NSArray *)list listCodes:(NSMutableArray *)arryCodes title:(NSString *)strTitle{
    
    [self.view endEditing:NO];
    BRStringPickerView *stringPickerView = [[BRStringPickerView alloc]initWithPickerMode:BRStringPickerComponentSingle];
    stringPickerView.title = strTitle;
    stringPickerView.dataSourceArr = list;
    WS(ws);
    stringPickerView.resultModelBlock = ^(BRResultModel *resultModel) {

            if (resultModel.index == 0) {
                ws.labLossUnit.text = resultModel.selectValue;
                ws.strLossCode = arryCodes[resultModel.index];
                ws.fleidLoss.text = @"0";
                ws.fleidLoss.enabled = NO;
                ws.fleidLoss.backgroundColor = [UIColor cjBackgroundColor];
            }else{
                ws.labLossUnit.text = resultModel.selectValue;
                ws.strLossCode = arryCodes[resultModel.index];
                ws.fleidLoss.enabled = YES;
                ws.fleidLoss.backgroundColor = [UIColor cjWhiteColor];
                ws.fleidLoss.text = @"";
            }
    };
    
    BRPickerStyle *customStyle = [BRPickerStyle pickerStyleWithThemeColor:[UIColor cjMainColor]];
    stringPickerView.pickerStyle = customStyle;
    [stringPickerView show];
}

- (void)btnFreightCostUnitAction{
//    [CJAlert showStringDismissWithDelay:@"运费单价"];
       [self btnFreightCostUnitActionType:@"1"];
}


- (void)btnSelectGoodTypeDicts:(CJGetSendGoodsType )type{
    [self.view endEditing:YES];
    WS(ws);
    [CJNetWork getSenGoodsDicsTypeinfo:type Success:^(id  _Nullable responseObject) {
        
        DLog(@"====== %@",responseObject);
//        [ws btnSelectTypeAction];
        NSDictionary *dic = responseObject[0];
        NSMutableArray *arrMutar = [[NSMutableArray alloc]init];
        NSMutableArray *arrMutarCodes = [[NSMutableArray alloc]init];

        [dic[@"dictList"] enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
            [arrMutar addObject:obj[@"name"]];

            
            switch (type) {
                case CJGetSendgoodsInsureType:
                    [arrMutarCodes addObject:obj[@"code"]];

                    break;
                default:
                    [arrMutarCodes addObject:obj[@"code"]];

                    break;
            }
            
            DLog(@"===== %@",arrMutarCodes);
            
        }];

        [ws btnSelectType:type list:arrMutar listCodes:arrMutarCodes title:dic[@"name"]];
        
    }];
    
}

- (void)btnSelectType:(CJGetSendGoodsType )type list:(NSArray *)list listCodes:(NSMutableArray *)arryCodes title:(NSString *)strTitle{
    
    [self.view endEditing:NO];
    BRStringPickerView *stringPickerView = [[BRStringPickerView alloc]initWithPickerMode:BRStringPickerComponentSingle];
    stringPickerView.title = strTitle;
    stringPickerView.dataSourceArr = list;
    WS(ws);
    stringPickerView.resultModelBlock = ^(BRResultModel *resultModel) {

        switch (type) {
            case CJGetSendgoodsTransferFeeUnits:
                ws.labGoodsTransferFeeUnits.text = resultModel.selectValue;
                ws.strGoodsTransferFeeUnits = arryCodes[resultModel.index];
                
                if ([arryCodes[resultModel.index] intValue] == 2) {
                    [self setFieldGoodsNumThreeDecimalPlace:NO];
                    self.fieldGoodsNum.text = @"";
                }else{
                    [self setFieldGoodsNumThreeDecimalPlace:YES];
                }
                
                ws.labGoodsPriceUnit.text = CJStringWithFormat(@"元/%@",resultModel.selectValue);
                ws.labHighestPriceUnit.text = CJStringWithFormat(@"元/%@",resultModel.selectValue);

                // 设置合理损耗不计
                
                ws.strLossCode = arryCodes[resultModel.index]; //
                if (![ws.strLossCode isEqualToString:@"0"]) {
                    ws.strLossCode = CJStringWithFormat(@"%@0",ws.strLossCode);
                }
                
                ws.fleidLoss.enabled = NO;
                ws.fleidLoss.backgroundColor = [UIColor cjBackgroundColor];
                ws.fleidLoss.text = @"0";
                ws.labLossUnit.text = @"不计";
                
                [ws btnFreightCostUnitActionType:@"0"];
                break;
            case CJGetSendgoodslimitWastageType:
                
                if (resultModel.index == 0) {
                    ws.labLossUnit.text = resultModel.selectValue;
                    ws.strLossCode = arryCodes[resultModel.index];
                    ws.fleidLoss.text = @"0";
                    ws.fleidLoss.enabled = NO;
                    ws.fleidLoss.backgroundColor = [UIColor cjBackgroundColor];
                }else{
                    ws.labLossUnit.text = resultModel.selectValue;
                    ws.strLossCode = arryCodes[resultModel.index];
                     ws.fleidLoss.enabled = YES;
                    ws.fleidLoss.backgroundColor = [UIColor cjWhiteColor];
                    ws.fleidLoss.text = @"";
                }
//                ws.labLossUnit.text = resultModel.selectValue;
//                ws.strLossCode = arryCodes[resultModel.index];
                break;
            case CJGetSendgoodsInsureType:
                ws.strInSureMoneyCode = arryCodes[resultModel.index];
                ws.fieldInSureMoney.text = resultModel.selectValue;

                break;
//            迭代去掉 21-08-13
//            case CJGetSendgoodsBillingType:
//                ws.strOpenInvoiceType = arryCodes[resultModel.index];
//                ws.fieldOpenInvoiceType.text = resultModel.selectValue;
//                break;
//

                
            default:
                break;
        }
        
    };
    
    BRPickerStyle *customStyle = [BRPickerStyle pickerStyleWithThemeColor:[UIColor cjMainColor]];
    stringPickerView.pickerStyle = customStyle;
    [stringPickerView show];
}

- (void)btnFreightCostUnitActionType:(NSString *)strType{
    
        NSString *strUrl = CJStringWithFormat(@"%@/ntocc-basic-api/dict/listDict",CJBaseUrl);
        NSDictionary *dicPra = @{
                @"types":CJStringWithFormat(@"goodsTransferFeeUnits%@",self.strGoodsTransferFeeUnits)
                                };
        WS(ws);
        [[HDNetworking sharedHDNetworking] GET:strUrl parameters:dicPra success:^(id  _Nonnull responseObject) {
                    
            DLog(@"===== %@",responseObject);
            
            NSDictionary *dic = responseObject[0];
            NSMutableArray *arrMutar = [[NSMutableArray alloc]init];
            NSMutableArray *arrMutarCodes = [[NSMutableArray alloc]init];

            if ([strType isEqualToString:@"1"]) {
                
                // 运费单价
                [dic[@"dictList"] enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
                    [arrMutar addObject:obj[@"name"]];
                    [arrMutarCodes addObject:obj[@"code"]];
                }];
                [ws btnSelectTypeList:arrMutar listCodes:arrMutarCodes title:dic[@"name"]];
            }else{
                NSArray *array = dic[@"dictList"];
                if (array.count > 0) {
                    NSDictionary *dic =  array[0];
                    ws.labFreightCostUnit.text = dic[@"name"];
                    ws.strFreightCostUnit = dic[@"code"];
                }
            }
            
        } failure:^(NSError * _Nonnull error) {
            
        }];
    
}

/// 运费单价
/// @param list <#list description#>
/// @param arryCodes <#arryCodes description#>
/// @param strTitle <#strTitle description#>
- (void)btnSelectTypeList:(NSArray *)list listCodes:(NSMutableArray *)arryCodes title:(NSString *)strTitle{
    
    [self.view endEditing:NO];
    BRStringPickerView *stringPickerView = [[BRStringPickerView alloc]initWithPickerMode:BRStringPickerComponentSingle];
    stringPickerView.title = strTitle;
    stringPickerView.dataSourceArr = list;
    WS(ws);
    stringPickerView.resultModelBlock = ^(BRResultModel *resultModel) {
        
        ws.labFreightCostUnit.text = resultModel.selectValue;
        ws.labHighestPriceUnit.text = resultModel.selectValue;
        ws.strFreightCostUnit = arryCodes[resultModel.index];
        
        if ([ws.strFreightCostUnit isEqualToString:@"3"]) {
            
            ws.fleidLoss.text = @"0";
            ws.labLossUnit.text = @"不计";
            ws.fleidLoss.enabled = NO;
            ws.fleidLoss.backgroundColor = [UIColor cjBackgroundColor];
//            ws.touchViewLoss.userInteractionEnabled = NO;
            ws.strLossCode = @"0";
        }else{
//            ws.touchViewLoss.userInteractionEnabled = YES;
            
        }
    };
    
    BRPickerStyle *customStyle = [BRPickerStyle pickerStyleWithThemeColor:[UIColor cjMainColor]];
    stringPickerView.pickerStyle = customStyle;
    [stringPickerView show];
}



/// 是否投保
/// @param sender <#sender description#>
-(void)switchInSureAction:(UISwitch *)sender
{
//viewInSure
     if([sender isOn]){
         NSLog(@"turn on");
         self.strIsInsure = @"1";
         self.viewInSureMoney.hidden = NO;
     }else{
         NSLog(@"turned off");
         self.strIsInsure = @"0";
         self.viewInSureMoney.hidden = YES;
         self.strInSureMoneyCode =  @"0";
         self.fieldInSureMoney.text = @"";
     }

}

//fieldInSureMoney; // 投保金额
- (void)btnInSureMoneyAction{
//   [CJAlert showStringDismissWithDelay:@"投保金额"];
    [self btnSelectGoodTypeDicts:CJGetSendgoodsInsureType];

}


// fieldUserCarType; //用车类型
- (void)btnUserCarTypeAction{
//   [CJAlert showStringDismissWithDelay:@"用车类型"];
    UseCarTypesVC *vc = [[UseCarTypesVC alloc]init];
    
    if (IsNilOrNull(self.dicUserCarType)) {
        vc.dictData = [[NSDictionary alloc]init];
    }else{
        vc.dictData = self.dicUserCarType;
    }
    
    WS(ws);
    vc.blockSelectInfo = ^(id anyObject) {
//        DLog(@"====== %@",anyObject);
        NSDictionary *dic = (NSDictionary *)anyObject;
        ws.fieldUserCarType.text = dic[@"index"];
        ws.dicUserCarType = dic;
    };
    [self pushVc:vc];
}
//fieldOpenInvoiceType 迭代去掉 21-08-13
//- (void)btnOpenInvoiceTypeAction{
////    [CJAlert showStringDismissWithDelay:@"开票方式"];
//    [self btnSelectGoodTypeDicts:CJGetSendgoodsBillingType];
//
//}




///  是否允许报价
/// @param sender <#sender description#>
-(void)switchIsAllowPriceAction:(UISwitch *)sender
{

     if([sender isOn]){
         NSLog(@"turn on");
         
         self.strIsQuote = @"1";
         self.viewHighestPrice.hidden = NO;
     }else{
         NSLog(@"turned off");
         self.strIsQuote = @"0";
         self.viewHighestPrice.hidden = YES;

     }
//    [self createTableView];
}

- (void)btnHighestPriceAction{
//    [CJAlert showStringDismissWithDelay:@"最高价单位"];
}


- (void)setFieldLimit{
    
    //货物数量
//    self.fieldGoodsNum.textFieldType = CJTextFieldStringTypeMoneyValue;
//    self.fieldGoodsNum.maxLength = CJTextLengthTen;
    [self setFieldGoodsNumThreeDecimalPlace:YES];
    //货物单价
    self.fieldGoodsPrice.textFieldType = CJTextFieldStringTypeMoneyValue;
    self.fieldGoodsPrice.maxLength = CJTextLengthTen;
    // 合理损耗
    self.fleidLoss.textFieldType = CJTextFieldStringTypeMoneyValue;
    self.fleidLoss.maxLength = CJTextLengthTen;
    // 运费单价
    self.fieldFreightCost.textFieldType = CJTextFieldStringTypeMoneyValue;
    self.fieldFreightCost.maxLength = CJTextLengthTen;
//    // 车数要求
//    self.fieldCarNumsRequir.textFieldType = CJTextFieldStringTypeNumber;
//    self.fieldCarNumsRequir.maxLength = CJTextLengthTen;
    //运输要求
    self.fieldTranRequirement.textFieldType = CJTextFieldStringTypeNone;
    self.fieldTranRequirement.maxLength = CJTextLengthMax;
    // 最高价
    self.fieldHighestPrice.textFieldType = CJTextFieldStringTypeMoneyValue;
    self.fieldHighestPrice.maxLength = CJTextLengthTen;
//    //应收
//    self.fieldUpstreamInFee.textFieldType = CJTextFieldStringTypeMoneyValue;
//    self.fieldUpstreamInFee.maxLength = 20;
    
}

/// 货物数鲁是否三位小数
/// @param isYes <#isYes description#>
- (void)setFieldGoodsNumThreeDecimalPlace:(BOOL )isYes{
    
    if (isYes) {
        self.fieldGoodsNum.textFieldType = CJTextFieldStringTypeThreeDecimalPlaces;
        self.fieldGoodsNum.maxLength = 16;
    }else{
        self.fieldGoodsNum.textFieldType = CJTextFieldStringTypeNumber;
        self.fieldGoodsNum.maxLength = 20;
    }
}



- (void)saveDefaultValueAction{

    NSMutableDictionary *dicData = [[NSMutableDictionary alloc]init];


    if ([CJRegular getIsStrEmpty:self.fieldGoodsNum.text]) {
        self.fieldGoodsNum.text = @"0";
    }
    
    if ([CJRegular getIsStrEmpty:self.fieldGoodsPrice.text]) {
        self.fieldGoodsPrice.text = @"0";
    }
    
    if ([CJRegular getIsStrEmpty:self.fleidLoss.text]) {
        self.fleidLoss.text = @"0";
        self.labLossUnit.text = @"不计";
        self.fleidLoss.enabled = NO;
        self.fleidLoss.backgroundColor = [UIColor cjBackgroundColor];
    }
    if ([CJRegular getIsStrEmpty:self.fieldFreightCost.text]) {
        self.fieldFreightCost.text = @"0";
    }
    
    if ([CJRegular getIsStrEmpty:self.fieldHighestPrice.text]) {
        self.fieldHighestPrice.text  = @"0";
    }
    [dicData setValue:self.strGoodsBigTypeCode forKey:@"coalBigType"];//optional): 货物名称编码（大类） ,
    [dicData setValue:self.strGoodsBigTypeName forKey:@"coalBigTypeName"];
    [dicData setValue:self.strGoodsNameCodes forKey:@"coalType"];// 货物名称名称（小类） ,
    [dicData setValue:self.fieldGoodsName.text forKey:@"coalTypeName"];
    
    if ([self.strGoodsNameCodes isEqualToString:@""]) {
        [CJAlert showStringDismissWithDelay:@"请选择货物名称"];
        return;
    }
    [dicData setValue:self.fieldGoodsNum.text forKey:@"weight"];//货物数量
    [dicData setValue:self.strGoodsTransferFeeUnits forKey:@"coalUnit"];//货物单位 0：吨 1：立方米，2-件 ,
    [dicData setValue:self.labGoodsTransferFeeUnits.text forKey:@"coalUnitName"];//货物单位 0：吨 1：立方米，2-件 ,
    
    [dicData setValue:self.fieldGoodsPrice.text forKey:@"coalPrice"];// 货物价值单价 b
    
    [dicData setValue:self.fleidLoss.text forKey:@"limitWastageAmount"];// 货物损耗数量 /kg
    [dicData setValue:self.labLossUnit.text forKey:@"limitWastageTypeName"];// 货物损耗数量 /kg
    [dicData setValue:self.strLossCode forKey:@"limitWastageType"];// 0-定额kg/车，1-定率千分之几/车，2-不计路耗

    [dicData setValue:self.labFreightCostUnit.text forKey:@"isTrunkName"];
    [dicData setValue:self.strFreightCostUnit forKey:@"isTrunk"]; //货物运费单位：0-元/吨，1-元/方，2-元/件，3-元/车
    [dicData setValue:self.fieldFreightCost.text forKey:@"price"];//成交单价--非含税价--司机单价 , 运费单价
    
    
    [dicData setValue:self.strIsInsure forKey:@"isInsure"];// 投保类别：0-不投保
    [dicData setValue:self.strInSureMoneyCode forKey:@"insuranceAmount"];//保额
    [dicData setValue:self.fieldInSureMoney.text forKey:@"insuranceFeeName"];
    [dicData setValue:self.strInSureMoneyCode forKey:@"insuranceCode"]; //insuranceCode
    [dicData setValue:self.strInSureMoneyCode forKey:@"insuranceFee"]; //单车保险费用

//    [dicData setValue:self.strAppointDriverCode forKey:@"pushTarget"];// 指定司机 fieldAppointDriver
//    [dicData setValue:self.fieldAppointDriver.text forKey:@"pushTargetName"];// 指定司机

    [dicData setValue:self.fieldUserCarType.text forKey:@"pushVehicleUse"]; // 用车类型
    
//    迭代去掉 21-08-13
//    [dicData setValue:self.strOpenInvoiceType forKey:@"billingType"];// 开票方式
//    [dicData setValue:self.fieldOpenInvoiceType.text forKey:@"billingTypeName"];//
    
    [dicData setValue:self.fieldTranRequirement.text forKey:@"coalName"]; // 运输要求

    [dicData setValue:self.strIsQuote forKey:@"isQuote"];
    [dicData setValue:self.fieldHighestPrice.text forKey:@"maxPrice"];
    
    DLog(@" dicData =  %@",dicData);
    
    
    NSString *strURL = CJStringWithFormat(@"%@/ntocc-tms-api/orderDefaultInfo",CJBaseUrl);
  
    WS(ws);
    
    if (self.isChange) {
        [dicData setValue:self.model.code forKey:@"code"];

        [[HDNetworking sharedHDNetworking] PUT:strURL parameters:@{@"orderDefaultDTO":[CJControl convertToJsonDataFromDic:dicData]} success:^(id  _Nonnull responseObject) {
            
            DLog(@"++++++++%@",responseObject);
            
          
            
            if (ws.blockSuccess) {
                ws.blockSuccess(@"");
                [ws pop];
            }
        } failure:^(NSError * _Nonnull error) {
            DLog(@"++++++++%@",error);
            
        }];

    }else{
        
        [[HDNetworking sharedHDNetworking] POST:strURL parameters:@{@"orderDefaultDTO":[CJControl convertToJsonDataFromDic:dicData]} success:^(id  _Nonnull responseObject) {
            
            DLog(@"++++++++%@",responseObject);
            
            if (ws.blockSuccess) {
                ws.blockSuccess(@"");
                [ws pop];
            }
            
        } failure:^(NSError * _Nonnull error) {
            DLog(@"++++++++%@",error);
            
        }];

    }
    
}

- (void)btnDeleteDefaultAction{
    
    NSString *strURL = CJStringWithFormat(@"%@/ntocc-tms-api/orderDefaultInfo",CJBaseUrl);

    WS(ws);
    
    if ([CJRegular getIsStrEmpty:self.model.code]) {
        [self setDefaultValue];
        return;
    }
    [[HDNetworking sharedHDNetworking] DELETE:strURL parameters:@{@"codes":self.model.code} success:^(id  _Nonnull responseObject) {
        
        DLog(@"++++++++%@",responseObject);
        self.model = nil;
        [ws setDefaultValue];
        
    } failure:^(NSError * _Nonnull error) {
        DLog(@"++++++++%@",error);
        
    }];
}

- (void)setDefaultValue{
    
    self.dicGoodsName = [[NSDictionary alloc]init];
    self.dicUserCarType = [[NSDictionary alloc]init];
    // 名称
    self.strGoodsNameCodes = @"";
    self.fieldGoodsName.text = @"";
    self.strGoodsBigTypeCode = @"";
    self.strGoodsBigTypeName = @"";

    self.fieldGoodsNum.text = @"";
    
    self.strGoodsTransferFeeUnits  = @"0";
    self.labGoodsTransferFeeUnits.text = @"吨";
    
    self.fieldGoodsPrice.text = @"";
    self.labGoodsPriceUnit.text = @"元/吨";
    self.strLossCode = @"0"; // 损耗 默认2 buji
    self.fleidLoss.text = @"0";
    self.fleidLoss.enabled = NO;
    self.fleidLoss.backgroundColor = [UIColor cjBackgroundColor];
    self.labLossUnit.text = @"不计";
    self.strFreightCostUnit = @"0";
    self.labFreightCostUnit.text = @"元/吨";
    self.fieldFreightCost.text = @"";
    
    self.strIsInsure = @"0"; //投保类别：0-不投保
    self.switchInSure.on = NO;
    self.strInSureMoneyCode = @"0";
    
    self.strAppointDriverCode = @""; // 指定司机
//    self.fieldAppointDriver.text = @"";

    self.fieldUserCarType.text = @"";
//    迭代去掉 21-08-13
//    self.strOpenInvoiceType  = @"0";
//    self.fieldOpenInvoiceType.text = @"现付开票";
    self.fieldTranRequirement.text = @"";
    
    
    self.strIsInsure = @"0"; //投保类别：0-不投保
    self.switchInSure.on = NO;
    self.strInSureMoneyCode =  @"0";
    self.fieldInSureMoney.text = @"";
    self.fieldHighestPrice.text = @"";
    
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 0.01;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"SendGoodsCell";
    SendGoodsCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[SendGoodsCell alloc] initWithFlex:nil reuseIdentifier:identifier];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
    }

    return cell;

}


- (CJTableView *)tableView{
    if (!_tableView) {
        _tableView = CJTableView.tbInitFrameStyle(CGRectMake(0, kTopHeight + CJTableToTopHeight, SCREEN_WIDTH, SCREEN_HEIGHT-kTopHeight -CJTableToTopHeight),UITableViewStylePlain)
        .tbDelegate(self)
        .tbDataSource(self)
        .tbSetOther(YES)
        .tbEstimatedRowHeight(YES)
        ;

    }
    
    return _tableView;
}

@end
